package com.lottery.ruok.lottrey.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.ChangePasswordRequest;
import com.lottery.ruok.lottrey.retrofitModule.response.ChangePasswordResponse;
import com.lottery.ruok.lottrey.userInfo.UserInfo;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import glideHelp.GlideHelper;
import utils.Utils;

public class ResetPasswordActivity extends BaseActivity implements TextWatcher {

    @BindView(R.id.old_password)
    EditText oldPassword;
    @BindView(R.id.new_password)
    EditText newPassword;
    @BindView(R.id.confirm_new_password)
    EditText confirmNewPassword;
    @BindView(R.id.confirm)
    TextView confirm;
    @BindView(R.id.authcode)
    EditText authcode;
    @BindView(R.id.authimg)
    ImageView authimg;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_password);
        ButterKnife.bind(this);

        init();
    }

    private void init() {
        GlideHelper.loadImageWithoutCache(this, UserInfo.getQuickDomain() + "captcha", authimg);

        oldPassword.addTextChangedListener(this);
        newPassword.addTextChangedListener(this);
        confirmNewPassword.addTextChangedListener(this);
        authcode.addTextChangedListener(this);
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        if (oldPassword.getText().length() > 0 && newPassword.getText().length() > 0 && confirmNewPassword.getText().length() > 0 && authcode.getText().length() > 0) {
//            confirm.setBackgroundResource(R.drawable.bg_corners_yellow);
            confirm.setEnabled(true);
        } else {
//            confirm.setBackgroundResource(R.drawable.bg_corners_grey);
            confirm.setEnabled(false);
        }
    }

    @OnClick(R.id.confirm)
    public void onViewClicked() {
        if(newPassword.getText().length() < 6 || newPassword.getText().length() > 20 || confirmNewPassword.getText().length() < 6 ||confirmNewPassword.getText().length() > 20){
            Utils.showToast("密码不能小于6位或大于20位");
        } else if(!Utils.hasDigit(newPassword.getText().toString()) || !Utils.judgeContainsStr(newPassword.getText().toString()) || !Utils.hasDigit(confirmNewPassword.getText().toString()) || !Utils.judgeContainsStr(confirmNewPassword.getText().toString())){
            Utils.showToast("密码请包含字母和数字");
        } else {
            //sendAnalyticMsg("lottery_" + SetActivity.ANALYTIC_VERSION + "_me_setting_modifypwd_comfirm");

            RetrofitBase.getRetrofit().create(ChangePasswordRequest.class).getResult(UserInfo.getUsername(), oldPassword.getText().toString(), newPassword.getText().toString(), confirmNewPassword.getText().toString(), authcode.getText().toString(), UserInfo.getToken())
                    .compose(RxHelper.<ChangePasswordResponse>handleResult())
                    .subscribe(new RxSubscribe<ChangePasswordResponse>(this) {
                        @Override
                        protected void _onNext(ChangePasswordResponse changePasswordResponse) {
                            UserInfo.setToken(changePasswordResponse.getToken());

                            Utils.showToast(changePasswordResponse.getMsg());

                            finish();
                        }

                        @Override
                        protected boolean showDialog() {
                            return true;
                        }
                    });
        }
    }

    @OnClick(R.id.authimg)
    public void onViewClicked1() {
        GlideHelper.loadImageWithoutCache(this, UserInfo.getQuickDomain() + "captcha", authimg);
    }
}